package com.chengyu.core.controller.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chengyu.core.controller.UserBaseController;
import com.chengyu.core.domain.form.ParamsSearchForm;
import com.chengyu.core.entity.CommonResult;
import com.chengyu.core.exception.ServiceException;
import com.chengyu.core.mapper.SysConfigMapper;
import com.chengyu.core.mapper.SysWeixinConfigMapper;
import com.chengyu.core.model.*;
import com.chengyu.core.service.config.ParamsManager;
import com.chengyu.core.utils.HttpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author LeGreen
 * @title 多元化配置管理
 * @date 2021/1/8
 */
@Api(tags = "多元化配置")
@Controller
@RequestMapping("/common")
public class ConfigParamsController extends UserBaseController {

    @Autowired
    private ParamsManager paramsManager;
    @Autowired
    private SysWeixinConfigMapper sysWeixinConfigMapper;

    @ApiOperation(value = "多元化参数")
    @ResponseBody
    @RequestMapping(value = "/params/get", method = RequestMethod.GET)
    public CommonResult<Object> getParams(ParamsSearchForm form) {
        UmsMember member = getCurrentMemberOrNull();
        if (member != null) {
            form.setGroupId(member.getGroupId());
            form.setMissionConfigId(member.getMissionConfigId());
        }
        Object object = paramsManager.getParamsByKey(form);
        return CommonResult.success(object);
    }

    @ApiOperation(value = "获取微信配置")
    @ResponseBody
    @RequestMapping(value = "/params/wxConfig", method = RequestMethod.GET)
    public CommonResult<Object> wxConfig() throws ServiceException {
        SysWeixinConfigExample example = new SysWeixinConfigExample();
        List<SysWeixinConfig> sysWeixinConfigList = sysWeixinConfigMapper.selectByExample(example);
        if (sysWeixinConfigList.size() == 0) {
            throw new ServiceException("配置不存在");
        }
        return CommonResult.success(sysWeixinConfigList.get(0));
    }

    @ApiOperation(value = "从微信官方请求jscode2session")
    @ResponseBody
    @RequestMapping(value = "/params/jscode2session", method = RequestMethod.GET)
    public CommonResult<JSONObject> jscode2session(String appid, String secret, String js_code, String grant_type) throws ServiceException {
        Map map = new HashMap();
        map.put("appId", appid);
        map.put("secret", secret);
        map.put("js_code", js_code);
        map.put("grant_type", grant_type);
        String result = HttpUtil.doGetMap("https://api.weixin.qq.com/sns/jscode2session", map);
        JSONObject jsonObject = JSON.parseObject(result);
        return CommonResult.success(jsonObject);
    }
}